package dimension.starry.loot;

import dimension.starry.utils.RandomUtil;
import net.minecraft.item.Item;
import net.minecraft.item.Items;

import java.util.HashMap;
import java.util.Map;

public class FishLoots {

    private static final Map<Item, Integer> LOOT_WEIGHT_MAP = new HashMap<>() {{
        put(Items.TROPICAL_FISH, 100);
        put(Items.BLUE_ICE, 100);
        put(Items.BOWL, 500);
        put(Items.AIR, 9000);
        put(Items.COBBLESTONE, 300);
        put(Items.DIRT, 300);
        put(Items.FISHING_ROD, 100);
        put(Items.PUFFERFISH, 100);
        put(Items.COD, 100);
        put(Items.COOKED_COD, 100);
        put(Items.KELP, 100);
        put(Items.DRIED_KELP, 100);
        put(Items.SALMON, 100);
        put(Items.COOKED_SALMON, 100);
        put(Items.INK_SAC, 100);
        put(Items.GLOW_INK_SAC, 5);
        put(Items.SCUTE, 10);
        put(Items.PRISMARINE_SHARD, 10);
        put(Items.PRISMARINE_CRYSTALS, 5);
        put(Items.NAUTILUS_SHELL, 5);
        put(Items.HEART_OF_THE_SEA, 5);
        put(Items.TURTLE_HELMET, 5);
        put(Items.GLASS_BOTTLE, 500);
        put(Items.STICK, 500);
        put(Items.AXOLOTL_BUCKET, 100);
        put(Items.COD_BUCKET, 100);
        put(Items.PUFFERFISH_BUCKET, 100);
        put(Items.SALMON_BUCKET, 100);
        put(Items.TADPOLE_BUCKET, 100);
        put(Items.TROPICAL_FISH_BUCKET, 100);
    }};

    public static Item getRandomItem() {
        int totalWeight = LOOT_WEIGHT_MAP.values().stream().mapToInt(i -> i).sum();
        int random = RandomUtil.randomInt(1, totalWeight + 1), current = 0;
        for (Map.Entry<Item, Integer> entry : LOOT_WEIGHT_MAP.entrySet()) {
            if (random > current && random <= current + entry.getValue()) {
                return entry.getKey();
            }
            current += entry.getValue();
        }
        throw new RuntimeException(String.format("calculate error, weight not in range: total=%d random=%d current=%d",
            totalWeight,
            random,
            current
        ));
    }

}
